

**CHOSE INSTRUMENTS + SPEC
global Zbase = "lag_l lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D"
global INS =  "dm_lag_r dm_lag_l dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D"
global spec = "new_kl"
local spec = "$spec"

clear all
use "$working/prowess_wits_estimation_ronly_kl.dta", clear
		
xtset product_id year
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D ///
ln_wIPT_IND_CHN {
	gen lag_`v' = L.`v'
}
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN {
	drop if lag_`v'==. | `v'==.
}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in r lag_r k lag_k l lag_l I_sq lag_I_sq ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}
			
gmm (dm_r - {rho=0.9}*dm_lag_r - {beta_l=0.7}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) ///
	- {beta_k=0.2}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) ///
	- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN}), ///
	instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)

predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen beta_k_hat_r = [beta_k]_cons
gen beta_l_hat_r = [beta_l]_cons
gen beta_lk_hat_r = [beta_lk]_cons
		
gen beta_k_hat_r_se = _se[beta_k:_cons]
gen beta_l_hat_r_se = _se[beta_l:_cons]
gen beta_lk_hat_r_se = _se[beta_lk:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]
		
generate rts = beta_k_hat_r + beta_l_hat_r
		
lincom [beta_k]_cons+[beta_l]_cons
generate rts_se = r(se)
		
gen theta_k_hat_r= beta_k_hat_r + beta_lk_hat_r*(l - k)
gen theta_l_hat_r = beta_l_hat_r + beta_lk_hat_r*(k - l)
		
foreach variable in k_hat_r l_hat_r {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)
}


xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)
generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]


		
contract nic_08_2dig rho_hat rho_hat_se beta_k_hat_r beta_l_hat_r beta_lk_hat_r beta_lag_ln_IND beta_lag_ln_IND_se ///
	theta_k_hat_r_mean theta_k_hat_r_med theta_l_hat_r_mean theta_l_hat_r_med  theta_k_hat_r_sd theta_l_hat_r_sd rts rts_se ///
	beta_k_hat_r_se beta_l_hat_r_se beta_lk_hat_r_se beta_lag_MA beta_lag_MA_se	perc_test se_perc_test
	
rename _freq Observations
		
gen delta_hat = beta_k_hat_r + beta_l_hat_r
gen beta_k_hat_q = beta_k_hat_r/(beta_k_hat_r + beta_l_hat_r)
gen beta_l_hat_q = beta_l_hat_r/(beta_k_hat_r + beta_l_hat_r)
gen beta_lk_hat_q = beta_lk_hat_r/(beta_k_hat_r + beta_l_hat_r)


save "$working/full_ProdR_`spec'_L1", replace
*/


**CHOSE INSTRUMENTS + SPEC
global Zbase = "lag_l lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag2_l lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D"
global INS =  "dm_lag_r dm_lag2_r dm_lag_l dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D dm_lag2_l dm_lag2_D_ln_micro_inv dm_lag2_D_micro_inv dm_lag2_I_sq_micro_inv dm_lag2_I_sq_micro_inv_D"
local spec = "new_kl"

clear all
use "$working/prowess_wits_estimation_ronly_kl.dta", clear
		
xtset product_id year
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D ///
ln_wIPT_IND_CHN {
	gen lag_`v' = L.`v'
	gen lag2_`v' = L2.`v'
}
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN {
	drop if lag_`v'==. | `v'==. | lag2_`v'==. 
}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in r lag_r k lag_k l lag_l I_sq lag_I_sq ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN ///
 lag2_r lag2_k lag2_l lag2_I_sq lag2_ln_shares lag2_ln_IND lag2_ln_IVS lag2_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

gmm (dm_r - {rho=0.9}*dm_lag_r - {rho2=-0.1}*dm_lag2_r - {beta_l=0.7}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) + {rho2}*{beta_l}*(dm_lag2_l + dm_lag2_ln_shares) ///
	- {beta_k=0.2}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) + {rho2}*{beta_k}*(dm_lag2_k+ dm_lag2_ln_shares) ///
	- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq + {rho2}*{beta_lk}*dm_lag2_I_sq ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN}), ///
	instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
		
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]

gen rho_hat2 = [rho2]_cons
gen rho_hat2_se = _se[rho2:_cons]

gen beta_k_hat_r = [beta_k]_cons
gen beta_l_hat_r = [beta_l]_cons
gen beta_lk_hat_r = [beta_lk]_cons
		
gen beta_k_hat_r_se = _se[beta_k:_cons]
gen beta_l_hat_r_se = _se[beta_l:_cons]
gen beta_lk_hat_r_se = _se[beta_lk:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]

gen beta_lag2_ln_IND = _b[xb:dm_lag2_ln_IND]
gen beta_lag2_ln_IND_se = _se[xb: dm_lag2_ln_IND]

gen beta_lag2_MA = _b[xb: dm_lag2_ln_wIPT_IND_CHN]
gen beta_lag2_MA_se = _se[xb: dm_lag2_ln_wIPT_IND_CHN]

generate rts = beta_k_hat_r + beta_l_hat_r
		
lincom [beta_k]_cons+[beta_l]_cons
generate rts_se = r(se)
		
gen theta_k_hat_r= beta_k_hat_r + beta_lk_hat_r*(l - k)
gen theta_l_hat_r = beta_l_hat_r + beta_lk_hat_r*(k - l)
		
foreach variable in k_hat_r l_hat_r {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)
}


xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)
generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]



contract nic_08_2dig rho_hat rho_hat_se beta_k_hat_r beta_l_hat_r beta_lk_hat_r beta_lag_ln_IND beta_lag_ln_IND_se ///
	theta_k_hat_r_mean theta_k_hat_r_med theta_l_hat_r_mean theta_l_hat_r_med  theta_k_hat_r_sd theta_l_hat_r_sd rts rts_se ///
	beta_k_hat_r_se beta_l_hat_r_se beta_lk_hat_r_se beta_lag_MA beta_lag_MA_se	///
	perc_test se_perc_test ///
	rho_hat2 rho_hat2_se beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se
	
rename _freq Observations
		
gen delta_hat = beta_k_hat_r + beta_l_hat_r
gen beta_k_hat_q = beta_k_hat_r/(beta_k_hat_r + beta_l_hat_r)
gen beta_l_hat_q = beta_l_hat_r/(beta_k_hat_r + beta_l_hat_r)
gen beta_lk_hat_q = beta_lk_hat_r/(beta_k_hat_r + beta_l_hat_r)

save "$working/full_ProdR_new_kl_L2", replace


**CHOSE INSTRUMENTS + SPEC
global Zbase = "lag_l lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag2_l lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D lag3_l lag3_D_ln_micro_inv lag3_D_micro_inv lag3_I_sq_micro_inv lag3_I_sq_micro_inv_D"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D lag3_D_ln_micro_inv lag3_D_micro_inv lag3_I_sq_micro_inv lag3_I_sq_micro_inv_D"
global INS =  "dm_lag_r dm_lag2_r dm_lag3_r dm_lag_l dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D dm_lag2_l dm_lag2_D_ln_micro_inv dm_lag2_D_micro_inv dm_lag2_I_sq_micro_inv dm_lag2_I_sq_micro_inv_D dm_lag3_l dm_lag3_D_ln_micro_inv dm_lag3_D_micro_inv dm_lag3_I_sq_micro_inv dm_lag3_I_sq_micro_inv_D"
local spec = "new_kl"

clear all
use "$working/prowess_wits_estimation_ronly_kl.dta", clear
		
xtset product_id year
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D ///
ln_wIPT_IND_CHN {
	gen lag_`v' = L.`v'
	gen lag2_`v' = L2.`v'
	gen lag3_`v' = L3.`v'
}
			
foreach v in r k l I_sq ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN {
	drop if lag_`v'==. | `v'==. | lag2_`v'==. | lag3_`v'==. 
}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in r lag_r k lag_k l lag_l I_sq lag_I_sq ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN ///
 lag2_r lag2_k lag2_l lag2_I_sq lag2_ln_shares lag2_ln_IND lag2_ln_IVS lag2_ln_wIPT_IND_CHN ///
 lag3_r lag3_k lag3_l lag3_I_sq lag3_ln_shares lag3_ln_IND lag3_ln_IVS lag3_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

gmm (dm_r - {rho=0.9}*dm_lag_r - {rho2=-0.1}*dm_lag2_r -  {rho3=-0.05}*dm_lag3_r - {beta_l=0.7}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) + {rho2}*{beta_l}*(dm_lag2_l + dm_lag2_ln_shares) + {rho3}*{beta_l}*(dm_lag3_l + dm_lag3_ln_shares)  ///
	- {beta_k=0.2}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) + {rho2}*{beta_k}*(dm_lag2_k+ dm_lag2_ln_shares) + {rho3}*{beta_k}*(dm_lag3_k+ dm_lag3_ln_shares)   ///
	- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq + {rho2}*{beta_lk}*dm_lag2_I_sq + {rho3}*{beta_lk}*dm_lag3_I_sq ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN}), ///
	instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
		
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]

gen rho_hat2 = [rho2]_cons
gen rho_hat2_se = _se[rho2:_cons]

gen rho_hat3 = [rho3]_cons
gen rho_hat3_se = _se[rho3:_cons]


gen beta_k_hat_r = [beta_k]_cons
gen beta_l_hat_r = [beta_l]_cons
gen beta_lk_hat_r = [beta_lk]_cons
		
gen beta_k_hat_r_se = _se[beta_k:_cons]
gen beta_l_hat_r_se = _se[beta_l:_cons]
gen beta_lk_hat_r_se = _se[beta_lk:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]

gen beta_lag2_ln_IND = _b[xb:dm_lag2_ln_IND]
gen beta_lag2_ln_IND_se = _se[xb: dm_lag2_ln_IND]

gen beta_lag2_MA = _b[xb: dm_lag2_ln_wIPT_IND_CHN]
gen beta_lag2_MA_se = _se[xb: dm_lag2_ln_wIPT_IND_CHN]

gen beta_lag3_ln_IND = _b[xb:dm_lag3_ln_IND]
gen beta_lag3_ln_IND_se = _se[xb: dm_lag3_ln_IND]

gen beta_lag3_MA = _b[xb: dm_lag3_ln_wIPT_IND_CHN]
gen beta_lag3_MA_se = _se[xb: dm_lag3_ln_wIPT_IND_CHN]

generate rts = beta_k_hat_r + beta_l_hat_r
		
lincom [beta_k]_cons+[beta_l]_cons
generate rts_se = r(se)
		
gen theta_k_hat_r= beta_k_hat_r + beta_lk_hat_r*(l - k)
gen theta_l_hat_r = beta_l_hat_r + beta_lk_hat_r*(k - l)
		
foreach variable in k_hat_r l_hat_r {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)
}


xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)
generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]



contract nic_08_2dig rho_hat rho_hat_se beta_k_hat_r beta_l_hat_r beta_lk_hat_r beta_lag_ln_IND beta_lag_ln_IND_se ///
	theta_k_hat_r_mean theta_k_hat_r_med theta_l_hat_r_mean theta_l_hat_r_med  theta_k_hat_r_sd theta_l_hat_r_sd rts rts_se ///
	beta_k_hat_r_se beta_l_hat_r_se beta_lk_hat_r_se beta_lag_MA beta_lag_MA_se	///
	perc_test se_perc_test ///
	rho_hat2 rho_hat2_se rho_hat3 rho_hat3_se beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se beta_lag3_ln_IND beta_lag3_ln_IND_se beta_lag3_MA beta_lag3_MA_se
	
rename _freq Observations
		
gen delta_hat = beta_k_hat_r + beta_l_hat_r
gen beta_k_hat_q = beta_k_hat_r/(beta_k_hat_r + beta_l_hat_r)
gen beta_l_hat_q = beta_l_hat_r/(beta_k_hat_r + beta_l_hat_r)
gen beta_lk_hat_q = beta_lk_hat_r/(beta_k_hat_r + beta_l_hat_r)

save "$working/full_ProdR_new_kl_L3", replace
